# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： transform
Description :
Author : 'li'
date： 2022/6/15
Change Activity:
2022/6/15:
-------------------------------------------------
"""
import cv2
import numpy as np
import torch
from PIL import Image
from torchvision.transforms.functional import to_pil_image, to_tensor


def ndarray_img_to_pil(img):
    """
    Transformer img to pil type.

    Args:
        img: ndarray image.

    Returns:

    """
    return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))


def pil_to_ndarray(img):
    """
    pil to
    Args:
        img:

    Returns:

    """
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)


def tensor_to_pil(tensor):
    """

    Args:
        tensor:

    Returns:

    """
    return to_pil_image(tensor)


def pil_to_tensor(img):
    """

    Args:
        img:

    Returns:

    """
    return to_tensor(img)


def inverse_sigmoid(x, eps=1e-5):
    """Inverse function of sigmoid.

    Args:
        x (Tensor): The tensor to do the
            inverse.
        eps (float): EPS avoid numerical
            overflow. Defaults 1e-5.
    Returns:
        Tensor: The x has passed the inverse
            function of sigmoid, has same
            shape with input.
    """
    x = x.clamp(min=0, max=1)
    x1 = x.clamp(min=eps)
    x2 = (1 - x).clamp(min=eps)
    return torch.log(x1 / x2)
